<?php
declare (strict_types=1);

namespace app\exam\logic\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\common\helper\util\Page;
use app\common\logic\category\Category;
use app\exam\model\admin\ModuleRollup as ModuleRollupModel;
use app\exam\validate\admin\ModuleRollup as ModuleRollupValidate;
use think\Model;

/**
 * Class ModuleRollup
 *
 * @package app\exam\model\admin
 */
class ModuleRollup extends ModuleRollupModel
{
    /**
     * 获取套卷列表数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param array $map
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $map = [])
    {
        return $this->where($map)->paginate(Page::listRows());
    }

    /**
     * 根据ID获取套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int $id
     * @return Model
     */
    public function getRollupById(int $id): Model
    {
        return $this->findOrFail($id);
    }

    /**
     * 新建套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int   $moduleId
     * @param array $data
     */
    public function createRollup(int $moduleId, array $data)
    {
        $data['module_id'] = $moduleId;
        validate(ModuleRollupValidate::class)->scene('create')->check($data);

        $save = [
            'module_id'             => $moduleId,
            'title'                 => $data['title'],
            'intro'                 => $data['intro'] ?? '',
            'price'                 => $data['price'],
            'credit_redeem'         => $data['credit_redeem'] ?? 0,
            'credit_redeem_percent' => $data['credit_redeem_percent'] ?? 0,
        ];

        if (isset($data['category'])) {
            $save['category'] = $save['fullcategorypath'] = $data['category'];
        }
        $re = $this::create($save);
        if ($re->isEmpty()) {
            throw new LogicException('新建失败，请重试');
        }
        
        Log::write('新建套卷【' . $data['title'] . '/' . $re->id . '】', 'rollup', 'create', true);
    }

    /**
     * 编辑套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int   $rollupId
     * @param int   $moduleId
     * @param array $data
     */
    public function editRollup(int $rollupId, int $moduleId, array $data)
    {
        $rollup            = $this->getRollupById($rollupId);
        $data['id']        = $rollupId;
        $data['module_id'] = $moduleId;
        validate(ModuleRollupValidate::class)->scene('edit')->check($data);

        $rollup->title = $data['title'];
        $rollup->price = $data['price'] ?? 0;
        $rollup->intro = $data['intro'] ?? '';

        $rollup->credit_redeem          = $data['credit_redeem'] ?? 0;
        $rollup->credit_redeem_percent  = $data['credit_redeem_percent'] ?? 0;

        if (isset($data['category'])) {
            $category         = is_array($data['category']) ? $data['category'] : json_decode($data['category'], true);
            $rollup->category = $rollup->fullcategorypath = $category;
        }

        if (!$rollup->save()) {
            throw new LogicException('修改套卷失败');
        }
        
        Log::write('编辑套卷【' . $rollup->title . '/' . $rollupId . '】', 'rollup', 'update', true);
    }

    /**
     * 编辑某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/2
     *
     * @param int    $rollupId
     * @param string $field
     * @param        $value
     * @return Model
     */
    public function editRollupField(int $rollupId, string $field, $value)
    {
        // 信息
        $rollup = $this->getRollupById($rollupId);

        $rollup->$field = $value;

        if (!$rollup->save()) {
            throw new LogicException("编辑失败");
        }
        
        Log::write('编辑套卷【' . $rollup->title . '/' . $rollupId . '】', 'rollup', 'update', true);

        // 返回值
        return $rollup;
    }

    /**
     * 删除套卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/1
     *
     * @param int $rollupId
     */
    public function deleteRollup(int $rollupId)
    {
        $rollup = $this->getRollupById($rollupId);

        if (!$rollup->delete()) {
            throw new LogicException('删除成功');
        }
        
        Log::write('删除套卷【' . $rollup['title'] . '/' . $rollupId . '】', 'rollup', 'delete', true);
    }

    /**
     * 获取套卷分类
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-16
     * @param  int    $id [description]
     * @return [type]     [description]
     */
    public function getRollupCategory(int $id)
    {
        return $this->where('id', $id)->value('category');
    }
}